<?php
class LinksController extends AdminAppController
{
    public $uses = array('Admin.Link');

    public $paginate = array(
        'limit' => 25,
        'conditions' => array('Link.link_visible' => 1),
        'order' => array(
            'Link.link_rating' => 'desc',
        ),
    );

    public function index()
    {
        $data = $this->paginate('Link');
     
        $this->set(compact('data'));
    }


    public function add()
    {
        if ($this->request->is('post')) {
            try {
                $this->Link->saveLink($this->request->data['Link']);
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The Link has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Link->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
    }

    public function edit($id=null)
    {
        $info = $this->Link->findById($id);
        if ($this->request->is('get')) {
            $this->request->data = $info;
        } else {
            try {
                $isUpdate = true;
                $this->Link->saveLink($this->request->data['Link'], $isUpdate, $info['Link']);
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The Service has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Link->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
    }

    public function delete($id=null)
    {
        if ($id == null) {
            $this->redirect('/');
        }
        $data = array(
            'id' => $id,
            'link_visible' => 0 
        );
        $this->Link->save($data);
        $this->redirect('/admin/links');
    }
}
